﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Warehouse.UI.WinForm.Forms
{
    public partial class BrowseForm : Form
    {
        #region -----属性--------

        public string TitleText
        {
            get { return _titleText; }
            set
            {
                _titleText = value;
                this.Text = value;
            }
        }
        private string _titleText;

        protected bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                if (_isBusy) this.Cursor = Cursors.WaitCursor;
                //listPart.BusyState = value;
                if (!_isBusy) this.Cursor = Cursors.Default;
            }
        }
        private bool _isBusy;

        public BrowseState ListState
        {
            get { return _listState; }
            set
            {
                _listState = value;
                //listPart.ListState = value;
            }
        }
        private BrowseState _listState;

        protected DataGridViewColumn[] DataGridViewColumns
        {
            get { return _dataGridViewColumns; }
            set
            {
                _dataGridViewColumns = value;
                dataGridViewContent.AutoGenerateColumns = false;
                dataGridViewContent.Columns.Clear();
                dataGridViewContent.Columns.AddRange(value);
            }
        }
        private DataGridViewColumn[] _dataGridViewColumns;

        protected Timer timer1;
        protected BindingSource bs;

        #endregion -----------

        //--未实现----------
        protected virtual void InitLoad() { }
        protected virtual void InitShown() { }
        protected virtual void ClearFilter() { }

        protected virtual bool AddItem() { throw new NotImplementedException("需要重载"); }
        protected virtual bool EditItem() { throw new NotImplementedException("需要重载"); }
        protected virtual bool DeleItem() { throw new NotImplementedException("需要重载"); }
        protected virtual bool ViewItem() { throw new NotImplementedException("需要重载"); }

        //--已实现----------
        protected virtual IEnumerable GetFilteredItems() { return null; }

        protected virtual void Find()
        {
            this.SuspendLayout();
            var i = dataGridViewContent.CurrentRow == null ? 0 : dataGridViewContent.CurrentRow.Index;

            bs.DataSource = GetFilteredItems();
            dataGridViewContent.DataSource = bs.DataSource;

            if (i < dataGridViewContent.RowCount)
                dataGridViewContent.CurrentCell = dataGridViewContent.Rows[i].Cells[dataGridViewContent.Columns.GetFirstColumn(DataGridViewElementStates.Visible).Index];
            this.ResumeLayout();
        }
        protected virtual bool Export()
        {
            MessageBox.Show(dataGridViewContent.RowCount.ToString());
            return true;
        }
        public virtual void OnFilterChanged(object sender, EventArgs e)
        {
            if (timer1.Enabled == false) timer1.Enabled = true;
        }

        //--Constructor
        public BrowseForm()
        {
            InitializeComponent();

            dataGridViewContent.ReadOnly = true;

            timer1 = new Timer();
            //var att = new System.Configuration.AppSettingsReader();
            timer1.Interval = 400; //(int)att.GetValue("ListFindInterval", typeof(int));

            bs = new BindingSource();
        }

        private void BrowseForm_Load(object sender, EventArgs e)
        {
            InitLoad();
        }

        private void BrowseForm_Shown(object sender, EventArgs e)
        {
            InitShown();
            ClearFilter();
            Find();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            this.IsBusy = true;
            try
            {
                Find();
            }
            finally
            {
                this.IsBusy = false;
            }
        }

        private void btnAdvFilter_Click(object sender, EventArgs e)
        {

        }

        private void btnExport_Click(object sender, EventArgs e)
        {

        }

        private void btnSetting_Click(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            this.IsBusy = true;
            try
            {
                AddItem();
            }
            finally
            {
                this.IsBusy = false;
            }
        }

        private void btnEditItem_Click(object sender, EventArgs e)
        {
            this.IsBusy = true;
            try
            {
                EditItem();
            }
            finally
            {
                this.IsBusy = false;
            }
        }

        private void btnDeleItem_Click(object sender, EventArgs e)
        {
            this.IsBusy = true;
            try
            {
                DeleItem();
            }
            finally
            {
                this.IsBusy = false;
            }
        }

        private void btnViewItem_Click(object sender, EventArgs e)
        {
            this.IsBusy = true;
            try
            {
                ViewItem();
            }
            finally
            {
                this.IsBusy = false;
            }
        }

    }
}
